

<!DOCTYPE html>
<html lang="en" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/blogs/img/fluid.png">
  <link rel="icon" href="/blogs/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="DM">
  <meta name="keywords" content="">
  
    <meta name="description" content="在开发工作中，我们经常会用到NSTimer 来实现定时器功能。但用心留意的小伙伴儿应该注意到了一个问题：定时器受其他UI界面刷新或者手势问题会出现卡顿现象，这会导致定时器不准确。究其原因：就是线程等待导致的问题。这也是面试中的一个经典案例。具体原因：NSTimer 的runloop类型是NSDefaultRunloopMode 主线程中， 界面的刷新也在主线程中，UIScrollview滑动的过程">
<meta property="og:type" content="article">
<meta property="og:title" content="iOS 处理NSTimer不准问题的三种解决方案">
<meta property="og:url" content="http://dsz1992.gitee.io/blogs/2021/03/17/2021-03-17%20iOS%20%E5%A4%84%E7%90%86NSTimer%E4%B8%8D%E5%87%86%E9%97%AE%E9%A2%98%E7%9A%84%E4%B8%89%E7%A7%8D%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="在开发工作中，我们经常会用到NSTimer 来实现定时器功能。但用心留意的小伙伴儿应该注意到了一个问题：定时器受其他UI界面刷新或者手势问题会出现卡顿现象，这会导致定时器不准确。究其原因：就是线程等待导致的问题。这也是面试中的一个经典案例。具体原因：NSTimer 的runloop类型是NSDefaultRunloopMode 主线程中， 界面的刷新也在主线程中，UIScrollview滑动的过程">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2021-03-16T16:00:00.000Z">
<meta property="article:modified_time" content="2024-04-26T01:41:15.753Z">
<meta property="article:author" content="John Doe">
<meta property="article:tag" content="iOS">
<meta name="twitter:card" content="summary_large_image">
  
  
  
  <title>iOS 处理NSTimer不准问题的三种解决方案 - Hexo</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/blogs/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/blogs/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/blogs/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"dsz1992.gitee.io","root":"/blogs/","version":"1.9.7","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":{"measurement_id":null},"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/blogs/local-search.xml","include_content_in_search":true};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/blogs/js/utils.js" ></script>
  <script  src="/blogs/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 7.2.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/blogs/">
      <strong>Fluid</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blogs/" target="_self">
                <i class="iconfont icon-home-fill"></i>
                <span>Home</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blogs/archives/" target="_self">
                <i class="iconfont icon-archive-fill"></i>
                <span>Archives</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blogs/categories/" target="_self">
                <i class="iconfont icon-category-fill"></i>
                <span>Categories</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blogs/tags/" target="_self">
                <i class="iconfont icon-tags-fill"></i>
                <span>Tags</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blogs/about/" target="_self">
                <i class="iconfont icon-user-fill"></i>
                <span>About</span>
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              <i class="iconfont icon-search"></i>
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">
              <i class="iconfont icon-dark" id="color-toggle-icon"></i>
            </a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/blogs/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="GCD替代NSTimer"></span>
          
        </div>

        
          
  <div class="mt-3">
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2021-03-17 00:00" pubdate>
          March 17, 2021 am
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          680 words
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          6 mins
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <h1 id="seo-header">GCD替代NSTimer</h1>
            
            
              <div class="markdown-body">
                
                <h4 id="在开发工作中，我们经常会用到NSTimer-来实现定时器功能。但用心留意的小伙伴儿应该注意到了一个问题：定时器受其他UI界面刷新或者手势问题会出现卡顿现象，这会导致定时器不准确。究其原因：就是线程等待导致的问题。这也是面试中的一个经典案例。"><a href="#在开发工作中，我们经常会用到NSTimer-来实现定时器功能。但用心留意的小伙伴儿应该注意到了一个问题：定时器受其他UI界面刷新或者手势问题会出现卡顿现象，这会导致定时器不准确。究其原因：就是线程等待导致的问题。这也是面试中的一个经典案例。" class="headerlink" title="在开发工作中，我们经常会用到NSTimer 来实现定时器功能。但用心留意的小伙伴儿应该注意到了一个问题：定时器受其他UI界面刷新或者手势问题会出现卡顿现象，这会导致定时器不准确。究其原因：就是线程等待导致的问题。这也是面试中的一个经典案例。"></a>在开发工作中，我们经常会用到NSTimer 来实现定时器功能。但用心留意的小伙伴儿应该注意到了一个问题：定时器受其他UI界面刷新或者手势问题会出现卡顿现象，这会导致定时器不准确。究其原因：就是线程等待导致的问题。这也是面试中的一个经典案例。</h4><h4 id="具体原因：NSTimer-的runloop类型是NSDefaultRunloopMode-主线程中，-界面的刷新也在主线程中，UIScrollview滑动的过程中是在NSTrackingRunloopMode中，当我们在手指滑动过程中，系统会将NSDefaultRunloopMode-更改为NSTrackingRunloopMode，所以这也是为什么会出现NSTimer短暂暂停的原因"><a href="#具体原因：NSTimer-的runloop类型是NSDefaultRunloopMode-主线程中，-界面的刷新也在主线程中，UIScrollview滑动的过程中是在NSTrackingRunloopMode中，当我们在手指滑动过程中，系统会将NSDefaultRunloopMode-更改为NSTrackingRunloopMode，所以这也是为什么会出现NSTimer短暂暂停的原因" class="headerlink" title="具体原因：NSTimer 的runloop类型是NSDefaultRunloopMode 主线程中， 界面的刷新也在主线程中，UIScrollview滑动的过程中是在NSTrackingRunloopMode中，当我们在手指滑动过程中，系统会将NSDefaultRunloopMode 更改为NSTrackingRunloopMode，所以这也是为什么会出现NSTimer短暂暂停的原因"></a>具体原因：NSTimer 的runloop类型是NSDefaultRunloopMode 主线程中， 界面的刷新也在主线程中，UIScrollview滑动的过程中是在NSTrackingRunloopMode中，当我们在手指滑动过程中，系统会将NSDefaultRunloopMode 更改为NSTrackingRunloopMode，所以这也是为什么会出现NSTimer短暂暂停的原因</h4><h3 id="目前的解决方案有三种："><a href="#目前的解决方案有三种：" class="headerlink" title="目前的解决方案有三种："></a>目前的解决方案有三种：</h3><h4 id="1-通过更改RunloopMode-避免主线程阻塞"><a href="#1-通过更改RunloopMode-避免主线程阻塞" class="headerlink" title="1.通过更改RunloopMode 避免主线程阻塞"></a>1.通过更改RunloopMode 避免主线程阻塞</h4><p>因为要避免主线程阻塞 所以也就是要避免使用NSDefaultRunloopMode。<br>查看到runloopMode的类型，可以看到 NSCommonRunloopMode （伪模式）不常用，而且是作为一个通用存在的。所以我们只需将NSDefaultRunloopMode 改为NSCommonRunloopMode调用就可以了。</p>
<p>但有一点需要注意：如果有多个NSCommonRunloopMode 执行不同任务 还是会出现卡顿导致定时器不准确问题。</p>
<figure class="highlight objectivec"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs objectivec"><span class="hljs-keyword">self</span>.timer = [<span class="hljs-built_in">NSTimer</span> timerWithTimeInterval:<span class="hljs-number">1</span> target:<span class="hljs-keyword">self</span> selector:<span class="hljs-keyword">@selector</span>(showTime) userInfo:<span class="hljs-literal">nil</span> repeats:<span class="hljs-literal">YES</span>];<br>[[<span class="hljs-built_in">NSRunLoop</span> currentRunLoop] addTimer:<span class="hljs-keyword">self</span>.timer forMode:<span class="hljs-built_in">NSRunLoopCommonModes</span>];<br></code></pre></td></tr></table></figure>

<h4 id="2-将NSTimer放在子线程中，当需要刷新UI界面时-再在主线程中进行。"><a href="#2-将NSTimer放在子线程中，当需要刷新UI界面时-再在主线程中进行。" class="headerlink" title="2.将NSTimer放在子线程中，当需要刷新UI界面时 再在主线程中进行。"></a>2.将NSTimer放在子线程中，当需要刷新UI界面时 再在主线程中进行。</h4><p>这个方法是类似于将NSTimer 任务独立出来，也就是将定时和刷新UI的工作任务分开来。</p>
<figure class="highlight objectivec"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><code class="hljs objectivec">- (<span class="hljs-type">void</span>)timerMethod2 &#123;<br><br><span class="hljs-built_in">NSThread</span> *thread = [[<span class="hljs-built_in">NSThread</span> alloc] initWithTarget:<span class="hljs-keyword">self</span> selector:<span class="hljs-keyword">@selector</span>(newThread) object:<span class="hljs-literal">nil</span>];<br><br>[thread start];<br><br>&#125;<br><br> -(<span class="hljs-type">void</span>)newThread<br><br>&#123;<br><br>[<span class="hljs-built_in">NSTimer</span> scheduledTimerWithTimeInterval:<span class="hljs-number">1.0</span> target:<span class="hljs-keyword">self</span> selector:<span class="hljs-keyword">@selector</span>(showTime) userInfo:<span class="hljs-literal">nil</span> repeats:<span class="hljs-literal">YES</span>];<br><br>[[<span class="hljs-built_in">NSRunLoop</span> currentRunLoop] run];<br><br>&#125;<br><br></code></pre></td></tr></table></figure>
<p>这个方法是可以实现但会增加代码量，而且有一定的内存泄漏风险。<br>因为定时器的取消我们在delloc中进行的。如果对象没有完全销毁 就会导致内存泄漏问题出现。<br>所以比较推崇接下来的第三种方案。</p>
<h4 id="3-gcd实现定时器"><a href="#3-gcd实现定时器" class="headerlink" title="3.gcd实现定时器"></a>3.gcd实现定时器</h4><p>gcd完全不受runloop影响，也就是说不会收到手势和UI刷新影响。所以它比NSTimer 更加准确。</p>
<figure class="highlight objectivec"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><code class="hljs objectivec"><span class="hljs-type">int</span> count = <span class="hljs-number">0</span>;<br><br>    <span class="hljs-comment">// 获得队列</span><br>   <span class="hljs-built_in">dispatch_queue_t</span> queue = dispatch_get_main_queue();<br><br>   <span class="hljs-comment">// 创建一个定时器(dispatch_source_t本质还是个OC对象)</span><br>   <span class="hljs-keyword">self</span>.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, queue);<br><br>   <span class="hljs-comment">// 设置定时器的各种属性（几时开始任务，每隔多长时间执行一次）</span><br>   <span class="hljs-comment">// GCD的时间参数，一般是纳秒（1秒 == 10的9次方纳秒）</span><br>   <span class="hljs-comment">// 何时开始执行第一个任务</span><br>   <span class="hljs-comment">// dispatch_time(DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC) 比当前时间晚3秒</span><br>   dispatch_time_t start = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(<span class="hljs-number">1.0</span> * <span class="hljs-built_in">NSEC_PER_SEC</span>));<br>   uint64_t interval = (uint64_t)(<span class="hljs-number">1.0</span> * <span class="hljs-built_in">NSEC_PER_SEC</span>);<br>   dispatch_source_set_timer(<span class="hljs-keyword">self</span>.timer, start, interval, <span class="hljs-number">0</span>);<br><br>   <span class="hljs-comment">// 设置回调</span><br>   dispatch_source_set_event_handler(<span class="hljs-keyword">self</span>.timer, ^&#123;<br>       <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@&quot;------------%@&quot;</span>, [<span class="hljs-built_in">NSThread</span> currentThread]);<br>       count++;<br><br>       <span class="hljs-keyword">if</span> (count == <span class="hljs-number">4</span>) &#123;<br>           <span class="hljs-comment">// 取消定时器</span><br>           dispatch_cancel(<span class="hljs-keyword">self</span>.timer);<br>           <span class="hljs-keyword">self</span>.timer = <span class="hljs-literal">nil</span>;<br>       &#125;<br>   &#125;);<br><br>   <span class="hljs-comment">// 启动定时器</span><br>   dispatch_resume(<span class="hljs-keyword">self</span>.timer);<br>   <br></code></pre></td></tr></table></figure>





                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/blogs/tags/iOS/" class="print-no-link">#iOS</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>iOS 处理NSTimer不准问题的三种解决方案</div>
      <div>http://dsz1992.gitee.io/blogs/2021/03/17/2021-03-17 iOS 处理NSTimer不准问题的三种解决方案/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>Author</div>
          <div>DM</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>Posted on</div>
          <div>March 17, 2021</div>
        </div>
      
      
      
        <div class="license-meta-item">
          <div>Licensed under</div>
          <div>
            
              
              
                <a class="print-no-link" target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
                  <span class="hint--top hint--rounded" aria-label="BY - Attribution">
                    <i class="iconfont icon-by"></i>
                  </span>
                </a>
              
            
          </div>
        </div>
      
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/blogs/2020/02/05/2020-2-5-%E6%96%87%E5%AD%97%E5%AE%9E%E7%8E%B0%E7%82%B9%E5%87%BB%E8%B7%B3%E8%BD%AC-2020/" title="文字实现点击跳转">
                        <span class="hidden-mobile">文字实现点击跳转</span>
                        <span class="visible-mobile">Next</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header">
    <i class="iconfont icon-list"></i>
    <span>Table of Contents</span>
  </p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">Search</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">Keyword</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.4/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/blogs/js/events.js" ></script>
<script  src="/blogs/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/blogs/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.20.1/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init(Object.assign({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      scrollSmooth    : true,
      includeTitleTags: true,
      headingsOffset  : -boardTop,
    }, CONFIG.toc));
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }

    Fluid.events.registerRefreshCallback(function() {
      if ('tocbot' in window) {
        tocbot.refresh();
        var toc = jQuery('#toc');
        if (toc.length === 0 || !tocbot) {
          return;
        }
        if (toc.find('.toc-list-item').length > 0) {
          toc.css('visibility', 'visible');
        }
      }
    });
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.11/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));

    Fluid.events.registerRefreshCallback(function() {
      if ('anchors' in window) {
        anchors.removeAll();
        var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
        var res = [];
        for (var item of el) {
          res.push('.markdown-body > ' + item.trim());
        }
        if (CONFIG.anchorjs.placement === 'left') {
          anchors.options.class = 'anchorjs-link-left';
        }
        anchors.add(res.join(', '));
      }
    });
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/blogs/js/local-search.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/blogs/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">Blog works best with JavaScript enabled</div>
  </noscript>
</body>
</html>
